<template>
  <div class="search-location">
    <div class="img-map">
      <img src="/img/高德地图.png" />
    </div>
    <input
      v-model="keyword"
      class="search-input"
      type="text"
      placeholder="搜索位置 公交站 地铁站"
      @focus="handleFocus"
      @blur="handleBlur"
      @input="handleInput"
      id="searchinput"
    />
    <i class="iconfont icon-Fangdajing" @click="keywordSearch"></i>
    <div class="img-road" @click="changeShowLoading()">
      <img src="/img/road.png" alt="" />
    </div>

    <div class="search-list" :class="{ searchshow: showSearchList }">
      <ul>
        <li v-for="(item, index) in imgTitle" :key="item.id" @click="handleType(index)">
          <img :src="item.img" />
          <span>{{ item.title }}</span>
        </li>
      </ul>
      <div class="gaode-phone">
        <div class="left">
          <div class="one">高德地图手机版</div>
          <div class="two">高德地图 哪儿都熟</div>
          <div class="three">去官网下载</div>
        </div>
        <div class="right">
          <img src="/img/二维码.png" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useCounterStore } from '@/stores/counter.js'

const {changeShowLoading } = useCounterStore()

const showSearchList = ref(false) // 显示快捷列表
const keyword = ref('') // 输入关键字
const imgTitle = ref([
  { id: 1, title: '美食', img: '/img/food.png' },
  { id: 2, title: '酒店', img: '/img/hotel.png' },
  { id: 3, title: '景点', img: '/img/view.png' },
  { id: 4, title: '小区', img: '/img/house.png' },
])

// 聚焦和失焦时,切换显示快捷列表
const handleFocus = () => {
  showSearchList.value = true
}
const handleBlur = () => {
  setTimeout(() => {
    showSearchList.value = false
  }, 200)
}

const emit = defineEmits(['inputSearch', 'handleType', 'keywordSearch'])
// 输入关键字时,调用emit方法,并传入关键字,并同时防抖处理
const handleInput =(e) => {
  const keyword = e.target.value
  emit('inputSearch', keyword)
  if (keyword.trim()) {
    showSearchList.value = false
  } else {
    showSearchList.value = true
  }
}

// 点击事件:根据种类搜索
const handleType = (index) => {
  if (index === 0) {
    keyword.value = '美食'
  } else if (index === 1) {
    keyword.value = '酒店'
  } else if (index === 2) {
    keyword.value = '景点'
  } else if (index === 3) {
    keyword.value = '小区'
  }
  emit('handleType', index)
}
// 放大镜点击事件:根据关键字搜索
const keywordSearch = () => {
  if (keyword.value.trim()) {
    emit('keywordSearch', keyword.value)
  } else {
    alert('请输入关键字')
  }
}
</script>

<style lang="scss" scoped>
::placeholder {
  color: #ddd; /* 替换为你想要的颜色 */
}
.search-location {
  position: absolute;
  top: 20px;
  left: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  background: #fff;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.3);
  .img-map {
    display: flex;
    align-items: center;
    cursor: pointer;
    img {
      width: 45px;
      height: 40px;
    }
  }
  .search-input {
    width: 200px;
    height: 30px;
    border: none;
    outline: none;
  }
  i {
    position: relative;
    font-size: 25px;
    color: #666;
    cursor: pointer;
    &::after {
      position: absolute;
      top: 35px;
      content: '搜索';
      display: block;
      width: 40px;
      height: 30px;
      background: rgba(0, 0, 0, 0.7);
      opacity: 0;
      border-radius: 5px;
      font-size: 12px;
      text-align: center;
      line-height: 30px;
      color: #fff;
    }
    &:hover::after {
      opacity: 1;
    }
  }
  .img-road {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 45px;
    height: 40px;
    background: #f4f4f4;
    cursor: pointer;
    img {
      width: 20px;
    }
    &::after {
      position: absolute;
      top: 40px;
      content: '路线';
      display: block;
      width: 40px;
      height: 30px;
      background: rgba(0, 0, 0, 0.7);
      opacity: 0;
      border-radius: 5px;
      font-size: 12px;
      text-align: center;
      line-height: 30px;
      color: #fff;
    }
    &:hover::after {
      opacity: 1;
    }
  }
  .search-list {
    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.3);
    background: #fff;
    position: absolute;
    top: 45px;
    height: 0px;
    opacity: 0;
    pointer-events: none;
    transition: all 0.3s ease-in-out;
    &.searchshow {
      height: 220px;
      opacity: 1;
      pointer-events: auto;
    }
    ul {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 30px;
      margin: 10px 30px;
      li {
        list-style: none;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 5px;
        color: #666;
        cursor: pointer;
        img {
          width: 46px;
        }
        span {
          font-size: 13px;
        }
      }
    }
    .gaode-phone {
      display: flex;
      justify-content: space-between;
      padding: 20px;
      border-top: 1px dotted #999;
      .left {
        font-size: 12px;
        color: #666;
        .one {
          font-size: 16px;
          font-weight: 700;
          color: rgb(26, 134, 2455);
        }
        .three {
          text-align: center;
          line-height: 35px;
          color: #fff;
          width: 120px;
          height: 35px;
          background: rgb(26, 134, 2455);
          border-radius: 20px;
          margin-top: 10px;
          cursor: pointer;
        }
      }
      .right {
        img {
          width: 88px;
          height: 88px;
        }
      }
    }
  }
}
</style>
